package gui.panels.media;

import gui.panels.media.dnd.MediaDragGestureRecognizer;
import gui.panels.media.dnd.MediaDragSourceListener;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Rectangle;
import java.awt.datatransfer.Transferable;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DragGestureEvent;
import java.awt.dnd.DragGestureListener;
import java.awt.dnd.DragGestureRecognizer;
import java.awt.dnd.DragSource;
import java.awt.dnd.DragSourceDragEvent;
import java.awt.dnd.DragSourceDropEvent;
import java.awt.dnd.DragSourceEvent;
import java.awt.dnd.DragSourceListener;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetListener;
import java.awt.event.InputEvent;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.media.CannotRealizeException;
import javax.media.Manager;
import javax.media.NoPlayerException;
import javax.media.Player;
import javax.media.Time;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.BevelBorder;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.ImageIcon;
import javax.swing.JLabel;

import media.process.MediaSliceContainer;

public class MediaCapturePanel extends JPanel implements DragGestureListener, DragSourceListener 
{
	private JPanel pnlMediaVideo = null;
	private Player mediaPlayer = null;
	private URL mediaURL=null;
	private DragSource dragSource =null;
	private MediaSliceContainer msc = null;

	/**
	 * This method initializes this
	 * 
	 */
	private void initialize() {
		this.setLayout(null);
		this.setSize(new Dimension(353, 264));
		this.add(getPnlMediaVideo(), null);
	}

	public void initMedia(URL mediaURL){
		try
		{


			// create a player to play the media specified in the URL
			msc = new MediaSliceContainer(new File(mediaURL.getFile()), null, null);
			this.mediaURL=mediaURL;
			mediaPlayer = Manager.createRealizedPlayer( mediaURL );

			MediaDragSourceListener mdgs = new MediaDragSourceListener();
			DragSource dragSource = DragSource.getDefaultDragSource(  );

			MediaDragGestureRecognizer mdgr = new MediaDragGestureRecognizer(msc, mdgs);

			Component video = mediaPlayer.getVisualComponent();
			Component controls = mediaPlayer.getControlPanelComponent();

			// get the components for the video and the playback controls
			dragSource.createDefaultDragGestureRecognizer( 
					video,
					//Aqui indicamos los tipos de acciones posibles que queremos: descritas en la clase DndConstants
					DnDConstants.ACTION_COPY | DnDConstants.ACTION_MOVE |
					DnDConstants.ACTION_LINK,
					mdgr );
			// Component linea = mediaPlayer.
			if ( video != null ) 
				this.getPnlMediaVideo().add( video, BorderLayout.CENTER ); // add video component

			if ( controls != null ) 
				this.getPnlMediaVideo().add( controls, BorderLayout.SOUTH ); // add controls

			this.getPnlMediaVideo().revalidate();

			mediaPlayer.start(); // start playing the media clip
		} // end try
		catch ( NoPlayerException noPlayerException )
		{
			System.err.println( "No media player found" );
		} // end catch
		catch ( CannotRealizeException cannotRealizeException )
		{
			System.err.println( "Could not realize media player" );
		} // end catch
		catch ( IOException iOException )
		{
			System.err.println( "Error reading from the source" );
		} // end catch

	}

	public MediaCapturePanel( URL mediaURL )
	{
		setLayout( new BorderLayout() ); // use a BorderLayout
		dragSource = DragSource.getDefaultDragSource() ;
		// Use lightweight components for Swing compatibility
		Manager.setHint( Manager.LIGHTWEIGHT_RENDERER, true );
		this.setSize(new Dimension(342, 250));
		this.setSize(new Dimension(268, 155));
		initMedia(mediaURL);



		//dgr.setSourceActions(dgr.getSourceActions() & ~InputEvent.BUTTON3_MASK);

		/* First argument:  Component to associate the target with
		 * Second argument: DropTargetListener 
		 */

		initialize();

	}


	public MediaCapturePanel(){


		initialize();
	}


	/**
	 * This method initializes pnlMediaVideo	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getPnlMediaVideo() {
		if (pnlMediaVideo == null) {
			pnlMediaVideo = new PlayerGUIPanel();
			pnlMediaVideo.setBounds(new Rectangle(16, 13, 323, 242));
			
			
		
		}
		return pnlMediaVideo;
	}

	public static void main(String[] args){
		JFrame ventana = new JFrame();

		MediaCapturePanel mp = new MediaCapturePanel();
		ventana.getContentPane().add(mp);
		ventana.setSize(200, 200);
		ventana.setVisible(true);
	}


	public Player getPlayer() {
		// TODO Auto-generated method stub
		return mediaPlayer;
	}

	
	@Override
	public void dragGestureRecognized(DragGestureEvent arg0) {
		/*
		//TODO Loguear accion
		System.out.println("MediaPanel DragGestureRecognized");
		Long start = new Long(this.getTxtStartVideo().getText());
		Long stop = new Long(this.getTxtEndVideo().getText());
		File mediaFile = new File(mediaURL.getFile());
		MediaSliceContainer container = new MediaSliceContainer(mediaFile, start, stop);
		if (container != null) {


			Transferable transferable = (Transferable) container.getUserObject();

			Cursor cursor = DragSource.DefaultCopyNoDrop;
			int action = arg0.getDragAction();
			if (action == DnDConstants.ACTION_MOVE) 
				cursor = DragSource.DefaultMoveNoDrop;
			dragSource.startDrag(arg0, cursor, transferable, this);
		}
		*/

	}

	@Override
	public void dragDropEnd(DragSourceDropEvent dsde) {
		// TODO Auto-generated method stub

	}

	@Override
	public void dragEnter(DragSourceDragEvent dsde) {
		// TODO Auto-generated method stub

	}

	@Override
	public void dragExit(DragSourceEvent dse) {
		// TODO Auto-generated method stub

	}

	@Override
	public void dragOver(DragSourceDragEvent dsde) {
		// TODO Auto-generated method stub

	}

	@Override
	public void dropActionChanged(DragSourceDragEvent dsde) {
		// TODO Auto-generated method stub

	}
}  //  @jve:decl-index=0:visual-constraint="41,0"// end class MediaPanel
